/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.db.DbPool;
/*     */ import com.zimbra.cs.mime.ParsedDocument;
/*     */ import com.zimbra.cs.util.Zimbra;
/*     */ import com.zimbra.cs.wiki.WikiPage.WikiContext;
/*     */ import com.zimbra.cs.wiki.WikiTemplate;
/*     */ import com.zimbra.cs.wiki.WikiTemplate.Context;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.PrintStream;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class MigrateToDocuments
/*     */ {
/*     */   private Mailbox mbox;
/*     */   private OperationContext octxt;
/*     */   
/*  40 */   public void handleAccount(Account account)
/*  40 */     throws ServiceException { handleMailbox(MailboxManager.getInstance().getMailboxByAccount(account, true)); }
/*     */   
/*     */   public void handleMailbox(Mailbox mbox) throws ServiceException {
/*  43 */     this.mbox = mbox;
/*  44 */     this.octxt = new OperationContext(mbox);
/*  45 */     Folder root = mbox.getFolderByPath(this.octxt, "/");
/*  46 */     String path = "/.migrate-wiki";
/*  47 */     Folder destRoot = null;
/*     */     try {
/*  49 */       destRoot = mbox.getFolderByPath(this.octxt, path);
/*     */     }
/*     */     catch (Exception e) {}
/*  52 */     if (destRoot == null)
/*  53 */       destRoot = mbox.createFolder(this.octxt, path, new Folder.FolderOptions().setDefaultView(MailItem.Type.DOCUMENT));
/*  54 */     if (destRoot == null) {
/*  55 */       ZimbraLog.misc.warn("Can't create folder: %s", new Object[] { path });
/*  56 */       return;
/*     */     }
/*  58 */     moveToBackupFolder(root, destRoot);
/*  59 */     migrateFromBackupFolder(this.octxt, destRoot, root);
/*  60 */     mbox.delete(this.octxt, destRoot.getId(), MailItem.Type.FOLDER);
/*     */   }
/*     */   
/*     */   private void moveToBackupFolder(Folder from, Folder to) throws ServiceException {
/*  64 */     for (Folder source : from.getSubfolders(this.octxt))
/*  65 */       if (source.getDefaultView() == MailItem.Type.WIKI)
/*     */       {
/*  67 */         String path = to.getPath() + "/" + source.getName();
/*  68 */         Folder dest = null;
/*     */         try {
/*  70 */           dest = this.mbox.createFolder(this.octxt, path, new Folder.FolderOptions().setDefaultView(MailItem.Type.DOCUMENT));
/*     */         } catch (MailServiceException e) {
/*  72 */           if (e.getCode().equals("mail.ALREADY_EXISTS")) {
/*  73 */             dest = this.mbox.getFolderByPath(this.octxt, path);
/*  74 */             ZimbraLog.misc.warn("Backup folder already exists: %s", new Object[] { source.getName() });
/*     */             break label187; }
/*  76 */           ZimbraLog.misc.warn("Can't create backup folder: %s", new Object[] { path }); }
/*  77 */         continue;
/*     */         
/*     */ 
/*  80 */         moveToBackupFolder(source, dest); }
/*     */     label187:
/*  82 */     for (MailItem item : this.mbox.getItemList(this.octxt, MailItem.Type.WIKI, from.getId())) {
/*     */       try {
/*  84 */         this.mbox.move(this.octxt, item.getId(), MailItem.Type.WIKI, to.getId());
/*     */       } catch (MailServiceException e) {
/*  86 */         if (e.getCode().equals("mail.ALREADY_EXISTS")) {
/*  87 */           ZimbraLog.misc.warn("Item already exists: %s", new Object[] { item.getName() });
/*     */         } else {
/*  89 */           ZimbraLog.misc.warn("Can't move item %s to backup folder %s", new Object[] { item.getName(), to.getPath() });
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void migrateFromBackupFolder(OperationContext octxt, Folder from, Folder to) throws ServiceException {
/*  96 */     for (Folder source : from.getSubfolders(octxt)) {
/*  97 */       String path = to.getPath();
/*  98 */       if (!path.endsWith("/"))
/*  99 */         path = path + "/";
/* 100 */       path = path + source.getName();
/* 101 */       Folder sub = this.mbox.getFolderByPath(octxt, path);
/* 102 */       migrateFromBackupFolder(octxt, source, sub);
/*     */     }
/* 104 */     if (to.getDefaultView() == MailItem.Type.WIKI)
/* 105 */       this.mbox.migrateFolderView(octxt, to, MailItem.Type.DOCUMENT);
/* 106 */     for (MailItem item : this.mbox.getItemList(octxt, MailItem.Type.WIKI, from.getId())) {
/* 107 */       Document doc = (Document)item;
/* 108 */       Document main = null;
/*     */       try {
/* 110 */         main = (Document)this.mbox.getItemByPath(octxt, to.getPath() + "/" + doc.getName());
/*     */       } catch (Exception e) {
/* 112 */         ZimbraLog.misc.info("Creating new item: " + doc.getName());
/*     */       }
/* 114 */       for (int rev = 1; rev < doc.getVersion(); rev++) {
/* 115 */         Document revision = null;
/*     */         try {
/* 117 */           revision = (Document)this.mbox.getItemRevision(octxt, item.getId(), MailItem.Type.DOCUMENT, rev);
/*     */         } catch (Exception e) {
/* 119 */           ZimbraLog.misc.warn("Can't get revision " + rev + " for item " + doc.getName(), e);
/*     */         }
/* 121 */         if (revision == null) {
/* 122 */           ZimbraLog.misc.warn("Empty revision " + rev + " for item " + doc.getName());
/*     */         }
/*     */         else
/*     */         {
/* 126 */           main = addRevision(item.getName(), main, revision, to);
/*     */         }
/*     */       }
/* 129 */       addRevision(item.getName(), main, doc, to);
/*     */     }
/*     */   }
/*     */   
/*     */   private Document addRevision(String name, Document main, Document revision, Folder to) {
/* 134 */     InputStream in = null;
/*     */     try {
/* 136 */       in = getContentStream(revision);
/* 137 */       String contentType = revision.getContentType();
/* 138 */       if (revision.getType() == MailItem.Type.WIKI)
/* 139 */         contentType = "application/x-zimbra-doc; charset=utf-8";
/* 140 */       ParsedDocument pd = new ParsedDocument(in, name, contentType, revision.getDate(), revision.getCreator(), revision.getDescription());
/* 141 */       if (main == null) {
/* 142 */         main = this.mbox.createDocument(this.octxt, to.getId(), pd, MailItem.Type.DOCUMENT, revision.getFlagBitmask());
/*     */       } else {
/* 144 */         this.mbox.addDocumentRevision(this.octxt, main.getId(), pd);
/*     */       }
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 154 */       return main;
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 147 */       ZimbraLog.misc.warn("Can't add new revision for " + name + " revision " + revision.getVersion(), e);
/*     */     } finally {
/* 149 */       if (in != null) {
/*     */         try {
/* 151 */           in.close();
/*     */         } catch (Exception e) {}
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private InputStream getContentStream(Document item) throws IOException, ServiceException {
/* 158 */     if (item.getType() == MailItem.Type.DOCUMENT)
/* 159 */       return item.getContentStream();
/* 160 */     String contents = new String(item.getContent(), "UTF-8");
/* 161 */     WikiTemplate wt = new WikiTemplate(contents, this.mbox.getAccountId(), Integer.toString(item.getFolderId()), item.getName());
/* 162 */     WikiTemplate.Context wctxt = new WikiTemplate.Context(new WikiPage.WikiContext(this.octxt, null), item, wt);
/* 163 */     return new ByteArrayInputStream(wt.toString(wctxt).getBytes("UTF-8"));
/*     */   }
/*     */   
/*     */   private static void usage() {
/* 167 */     System.out.println("zmwikimigrate [accountId]+");
/* 168 */     System.exit(0);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception {
/* 172 */     CliUtil.toolSetup();
/* 173 */     DbPool.startup();
/* 174 */     Zimbra.startupCLI();
/* 175 */     if (args.length == 0) {
/* 176 */       usage();
/*     */     }
/* 178 */     MigrateToDocuments migrate = new MigrateToDocuments();
/* 179 */     for (String arg : args) {
/* 180 */       Account account = Provisioning.getInstance().getAccountByName(arg);
/* 181 */       if (account == null) {
/* 182 */         System.out.println("Can't get account " + arg);
/*     */       }
/*     */       else {
/* 185 */         migrate.handleAccount(account);
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/MigrateToDocuments.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */